package cc.magicjson.example.demo.ali.app;


import cc.magicjson.example.demo.ali.app.domain.CityNode;
import cc.magicjson.example.demo.ali.app.domain.TreeNode;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;


import static cc.magicjson.example.demo.ali.util.FileUtils.readBufferFile;
import static java.lang.Long.parseLong;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.groupingBy;


public class FileSort {

    public static void main(String[] args) {
        // 1. 读取文件 获取文件信息
        String fileContent = readBufferFile("F:\\github-project\\demo\\file\\area.txt");
        // 2. 根据读取文件信息形成节点树集合对象
//        TreeSet<TreeNode> treeNodes = new TreeSet<>(comparing(TreeNode::getId));
        List<TreeNode> treeNodes = new ArrayList<>();
        // 3.按照换行符拆分 解析出父子节点关系
        for (String content : fileContent.split("\n")) {
            // 4.按照”-“拆分 数组[0]为父节点 取出
            String[] nodePath = content.split("-");
            // 临时存储父节点
            String parentNodeTmp = nodePath[0];
            String[] parentNode = parentNodeTmp.split(":");
            for (String cityStr : nodePath) {
                String[] node = cityStr.split(":");
                treeNodes.add(new TreeNode(parseLong(node[0]), parseLong(parentNode[0]), node[1]));
            }
        }
        System.out.println(treeNodes.size());
//        treeNodes.forEach(System.out::println);
        List<TreeNode> cityNodes = treeNodes.stream().sorted(comparing(TreeNode::getId)).collect(Collectors.toList());
        // 讲parentId一样的放入一个CityNode中
        Map<String, Map<Long, CityNode>> rootMap = new ConcurrentHashMap();
        Map<Long, CityNode> cityMap = new ConcurrentHashMap();
        AtomicReference<TreeNode> sourceNode = new AtomicReference<>(new TreeNode());
        cityNodes.stream()
                .collect(groupingBy(TreeNode::getParentId))
                .forEach((key, v) -> {
                    CopyOnWriteArrayList<TreeNode> copyOnWriteArrayList = new CopyOnWriteArrayList();
                    List<TreeNode> arrayList = v.stream().filter(distinctByKey(TreeNode::getId)).collect(Collectors.toList());
                    copyOnWriteArrayList.addAll(arrayList);
                    for (int i = 0; i < copyOnWriteArrayList.size(); i++) {
                        TreeNode treeNode = copyOnWriteArrayList.get(i);
                        if (treeNode.getId() == key) {
                            sourceNode.set(treeNode);
                        }
                    }
                    CityNode cityNode = new CityNode(key, sourceNode.get().getName(), copyOnWriteArrayList);
                    System.out.println(copyOnWriteArrayList);
                    cityMap.put(key, cityNode);
                });
        System.out.println(cityMap.size());
        rootMap.put("-1:中国", cityMap);
        for (Map.Entry entry : rootMap.entrySet()) {
            System.out.println(entry.getKey() + "\n");
            for (Map.Entry<Long, CityNode> cityNodeEntry : ((Map<Long, CityNode>) entry.getValue()).entrySet()) {
//                System.out.println(cityNodeEntry.getKey()+"L:"+cityNodeEntry.getValue().getTreeNode()+ "\n");
            }
        }
//        System.out.println(treeNodes.size());
    }

    private static void printNeedNode(TreeNode treeNode) {


    }

    private static <T> Predicate<T> distinctByKey(Function<? super T, ?> keyExtractor) {
        Set<Object> seen = ConcurrentHashMap.newKeySet();
        return t -> seen.add(keyExtractor.apply(t));
    }
}
